package com.sob.bountyhunter;

import java.util.Random;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Circle;
import org.newdawn.slick.geom.Rectangle;

import com.sob.bountyhunter.entidades.Entidad;

public class Particula extends Entidad{

	int width, height;
	long startTime;
	long duracion;
	float alpha;
	//Graphics graphics;

	public Particula(long time, float px, float py, float xv, float yv) throws SlickException {
		super();
		x=px;
		y=py;
		
		z=y+(10-(yv*10));
		
		width=20;
		height=20;
		
		duracion=time;
		
		xvel=xv;
		yvel=yv;
		xinc=xvel;
		yinc=yvel;
		
		alpha=1f;
		
		//tinte=Color.red;
		
		startTime=System.currentTimeMillis();
	}
	
	private void componer(Graphics graphics){
		fillBlack(graphics);
		clearCircle(graphics, width/2, height/2, width/2);
	}
	public void fillBlack(Graphics graphics){
		try {
			sprite=new Image(width, height);
		} catch (SlickException e) {
			e.printStackTrace();
		}
		/*
		try {
			Graphics gTemp=img.getGraphics();
			Graphics.setCurrent(gTemp);
			
			Rectangle r=new Rectangle(0, 0, width, height);
			gTemp.setColor(Color.black);
			gTemp.fill(r);
			
			//gTemp.setColor(Color.red);
			//gTemp.drawRect(0, 0, width, height);
			
			Graphics.setCurrent(graphics);
		} catch (SlickException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
	}
	
	public void clearCircle(Graphics graphics, float x, float y, float r){
		try {
			Graphics gTemp;
			gTemp = sprite.getGraphics();
			Graphics.setCurrent(gTemp);

			int maxAlpha=250;
			float paso=maxAlpha/r;
			Random rand=new Random();
			float pow=1; //rand.nextFloat();
			
			for(float i=r;i>=0;i--){				
				int val=(int)( (maxAlpha-(i*paso))/1 );
				gTemp.setColor(new Color(255 , 255, 255, val)); //val/1000));
				Circle c=new Circle(x, y, i);
				gTemp.fill(c);
			}

			Graphics.setCurrent(graphics);
		} catch (SlickException e) {
			e.printStackTrace();
		}

	}
	
	public void update(GameContainer gameContainer, int deltaTime, Mapa map){
		//super.update(gameContainer, deltaTime, map);
		x+=xinc*deltaTime;
		y+=yinc*deltaTime;
		
		//float paso=1f/duracion;
		//alpha-=((paso*deltaTime));
		if(System.currentTimeMillis()>startTime+(duracion/2)){
			float paso=1f/duracion;
			alpha-=((paso*deltaTime)*2);			
		}
		
		if(System.currentTimeMillis()>=startTime+duracion){
			delete=true;
		}
	}
	
	@Override
	public void render(GameContainer gameContainer, Graphics graphics, int xOff, int yOff) {
		if(sprite==null){
			componer(graphics);
		}
		
		//graphics.setDrawMode(Graphics.MODE_ADD);
		sprite.setAlpha(alpha);
		sprite.draw(x-(width/2)-xOff,y-(height/2)-yOff, tinte);
		//graphics.setDrawMode(Graphics.MODE_NORMAL);
	}
}
